package com.twp.imtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

public class LoginActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    private void connect(String token) {
        Log.e("LoginActivity", "connect: ");
        if (getApplicationInfo().packageName.equals(MyApp.getCurProcessName(getApplicationContext()))) {

            RongIM.connect(token, new RongIMClient.ConnectCallback() {

                /**
                 * Token 错误。可以从下面两点检查 1.  Token 是否过期，如果过期您需要向 App Server 重新请求一个新的 Token
                 *                  2.  token 对应的 appKey 和工程里设置的 appKey 是否一致
                 */
                @Override
                public void onTokenIncorrect() {
                    Log.e("LoginActivity", "onTokenIncorrect: ");
                }

                /**
                 * 连接融云成功
                 * @param userid 当前 token 对应的用户 id
                 */
                @Override
                public void onSuccess(String userid) {
                    Log.e("LoginActivity", "--onSuccess" + userid);
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("userid", userid);
                    RongIM.setUserInfoProvider(new UserInfoProvider(), true);
                    startActivity(intent);
                    finish();
                }

                /**
                 * 连接融云失败
                 * @param errorCode 错误码，可到官网 查看错误码对应的注释
                 */
                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                    Log.e("LoginActivity", "onError: " + errorCode.getValue() + ": " + errorCode.getMessage());
                }
            });
        } else {
            Log.e("LoginActivity", "else: ");
        }
    }

    public void goLogin1(View view) {
        Log.e("LoginActivity", "goLogin: ");
        connect(MyApp.token1);
    }

    public void goLogin2(View view) {
        Log.e("LoginActivity", "goLogin: ");
        connect(MyApp.token2);
    }
}
